﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TransportManager.UI.Pages;
using Microsoft.ApplicationBlocks.Data;
using Site.Components;
using System.IO;
using NPOI.HSSF.UserModel;
using System.Data;
using Site.Components.Entity;
using System.Collections;

namespace Site.Report
{
    /// <summary>
    /// 材料验收入库明细表 UI
    /// </summary>
    public partial class productinoutdetail : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }
        protected void btnMerge_Click(object sender, EventArgs e)
        {
            BindMergeData();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        public DataTable DataSource
        {
            get
            {
                var ds = SqlHelper.ExecuteDataset(Session["Conn"].ToString(), "Proc_ProductInOutDetail",
                 (object)(SearchTime1.StartTime.AsDateTime()), (object)SearchTime1.EndTime.AsDateTime());
                return ds.Tables[0];
            }
        }


        public IList<ProductInOutDetail> MergeDataSource
        {
            get
            {
               
                var ds = SqlHelper.ExecuteDataset(Session["Conn"].ToString(), "Proc_ProductInOutDetail",
                 (object)(SearchTime1.StartTime.AsDateTime()), (object)SearchTime1.EndTime.AsDateTime());
                DataTable dt= ds.Tables[0];

                var list = dt.ToList<ProductInOutDetail>();

                return list.Merge();
            }
        }

        void BindData()
        {

            gv.DataSource = DataSource;
            gv.DataBind();
        }

        void BindMergeData()
        {

            gv.DataSource = MergeDataSource;
            gv.DataBind();
        }



        protected void btnExport_Click(object sender, EventArgs e)
        {
            //读取模板
            FileStream fstream = new FileStream(MapPath("~/Template/productinoutdetailtemplate.xls"), FileMode.Open, FileAccess.Read);
            var hssfworkbook = new HSSFWorkbook(fstream);
            var sheet = hssfworkbook.GetSheetAt(0);

            string[] catalogs = new string[] { "金具", "线材", "塔材及加工件", "钢材",
                "绝缘材料", "主要设备", "硬母线", "电缆桥支架槽盒", "防火材料", "降阻剂", "耗材","工器具","汽配" };//设备分类排序,具体参考文档。用反射赋值

            for (int j = 0; j < DataSource.Rows.Count; j++)
            {
                var row = sheet.CreateRow(3+j); //从第3行写入数据
                DataRow dr= DataSource.Rows[j];
                row.CreateCell(0).SetCellValue(dr["checknumber"].ToString());
                row.CreateCell(1).SetCellValue(dr["ApplyCorp"].ToString());
                for (int i = 0; i < catalogs.Length; i++)
                {
                    row.CreateCell(i+2).SetCellValue(dr[catalogs[i]].ToString());                   
                }
            }
            MemoryStream ms = new MemoryStream();
            hssfworkbook.Write(ms);
         
            fstream.Close();

            string filename = string.Format("材料验收入库明细表-{0}到{1}.xls",SearchTime1.StartTime,SearchTime1.EndTime);
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8)));
            Response.Clear();
           
            Response.BinaryWrite(ms.GetBuffer());
            Response.End();
            hssfworkbook.Dispose();
        }

        protected void btnMergeExport_Click(object sender, EventArgs e)
        {
            //读取模板
            FileStream fstream = new FileStream(MapPath("~/Template/productinoutdetailtemplate.xls"), FileMode.Open, FileAccess.Read);
            var hssfworkbook = new HSSFWorkbook(fstream);
            var sheet = hssfworkbook.GetSheetAt(0);

            string[] catalogs = new string[] { "金具", "线材", "塔材及加工件", "钢材",
                "绝缘材料", "主要设备", "硬母线", "电缆桥支架槽盒", "防火材料", "降阻剂", "耗材","工器具","汽配" };//设备分类排序,具体参考文档。用反射赋值

            for (int j = 0; j < (MergeDataSource as IList).Count; j++)
            {
                var row = sheet.CreateRow(3 + j); //从第3行写入数据
                var obj = MergeDataSource[j];
                row.CreateCell(0).SetCellValue(obj.CheckNumber.ToString());
                row.CreateCell(1).SetCellValue(obj.ApplyCorp.ToString());
                for (int i = 0; i < catalogs.Length; i++)
                {
                    System.Reflection.PropertyInfo property = obj.GetType().GetProperty(catalogs[i]);
                    row.CreateCell(i + 2).SetCellValue(property.GetValue(obj,null).ToString());
                }
            }
            MemoryStream ms = new MemoryStream();
            hssfworkbook.Write(ms);

            fstream.Close();

            string filename = string.Format("材料验收入库明细表-{0}到{1}.xls", SearchTime1.StartTime, SearchTime1.EndTime);
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)));
            Response.Clear();

            Response.BinaryWrite(ms.GetBuffer());
            Response.End();
            hssfworkbook.Dispose();
        }
            
        
    }
}